package com.yy.smartBI.mqtest;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class FanOutProvider {
    private final static String QUEUE_NAME1 = "hello1";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        try (Connection connection = factory.newConnection();
             //创建两个消息队列
             Channel channel = connection.createChannel();
        ) {
            //声明交换机
            channel.exchangeDeclare("logs", "topic");
            //手动输入
            Scanner scanner = new Scanner(System.in);
            while (scanner.hasNextLine()) {
                String message = scanner.nextLine();
                String[] strs = message.split(" ");
                //将消息发送到交换机  不指定路由
                channel.basicPublish("logs", strs[1], null, message.getBytes(StandardCharsets.UTF_8));
                System.out.println(" [x] Sent '" + message + "'");
            }
        }
    }
}
